﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class UILS : ViewBase
    {
        public VLIEFDAT TMV
        {
            get
            {
                return (VLIEFDAT)MV;
            }
    }
        #region ==LIEFNR===========
        public static readonly DependencyProperty LIEFNRProperty = DependencyProperty.Register("LIEFNR",
            typeof(string), typeof(UILS),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFNRChanged));
        public string LIEFNR
        {
            get
            {
                return (string)GetValue(LIEFNRProperty);
            }
            set
            {
                SetValue(LIEFNRProperty, value);
                OnLIEFNRChg(value);
            }
        }
        private static void OnLIEFNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UILS;
            if (src != null)
            {
                src.LIEFNR = (string)e.NewValue;
            }
        }
        #endregion

        #region ==LSLIEFNA===========
        public static readonly DependencyProperty LSLIEFNAProperty = DependencyProperty.Register("LSLIEFNA",
            typeof(string), typeof(UILS),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLSLIEFNAChanged));
        public string LSLIEFNA
        {
            get
            {
                return (string)GetValue(LSLIEFNAProperty);
            }
            set
            {
                SetValue(LSLIEFNAProperty, value);
            }
        }
        private static void OnLSLIEFNAChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UILS;
            if (src != null)
            {
                src.LSLIEFNA = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwLS===========
        public static readonly DependencyProperty dwLSProperty = DependencyProperty.Register("dwLS",
            typeof(DataRow), typeof(UILS),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwLSChanged));
        public DataRow dwLS
        {
            get
            {
                return (DataRow)GetValue(dwLSProperty);
            }
            set
            {
                SetValue(dwLSProperty, value);
            }
        }
        private static void OdwLSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UILS;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwLS = row;
            }
        }
        #endregion

        public UILS()
        {
        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwLS = dwMaster;
            if (dwMaster == null)
            {
                LSLIEFNA = "";
                KEY = "";
            }
            else
            {
                LSLIEFNA = dwMaster["LSLIEFNA"].ToString();
                KEY =TMV.LSLIEFNR;
            }
        }

         
        
        public override void Save()
        {
            if (dwMaster == null)
                return;

            ((VLIEFDAT)MV).UpdateLIEFDAT(dwMaster, 0);
        }
       
        public override void Copy()
        {
            if (dwLS == null)
                return;
            DataRow dwData = TBMain.NewRow();
            dwData.ItemArray = dwLS.ItemArray;
            LIEFNR = VSDDAT.SD22.GETNEXT("0022", "  ", "LIEFDAT");
            dwData["LSLIEFNR"] = Helpers.Helper.Right("00000000" + LIEFNR, 8);
            TMV.Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
        } 

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            Copy();
            base.CopyCommand(sender, e);
        }
        public override void LOADBYAUF()
        { 
            if (ORDERNUM.Length < 8)
            {
                return;
            }
            if(dwMaster!=null)
            {
                if (TMV.LSLIEFNR == ORDERNUM.Substring(0, 8))
                    return;                  
            }
            TBMain.Clear();
            dwData = TBMain.NewRow();
            dwData["LSMANDANT"] = app.Mandant;
            dwData["LSLIEFNR"] = ORDERNUM.Substring(0, 8);
            SResult = ((VLIEFDAT)MV).QueryLIEFDAT(dwData, 1);
            MergeData(SResult, true);
            GetInfo();
            base.LOADBYAUF();
        }
        public virtual void GetInfo()
        {
            if (dwMaster == null)
                return;
            INFO = "";
            INFO +=Helpers.Helper.GetGB2312Value( dwMaster["LSLIEFNA"].ToString());
        }
        public virtual void OnLIEFNRChg(string value)
        {

           
        }
    }
}
